<?php

class Sinonim
{
    protected $pdo;
    
    public function __construct(\PDO $pdo = null)
    {
        if ($pdo == null) {
            //~ $dsn      = 'mysql:dbname=qas_quran; host=localhost; port=3306';
            $dsn      = 'mysql:dbname=sinonims; host=localhost; port=3306';
            $username = 'root';
            $password = 'kosong';
            $options  = array(
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
            ); 

            try {
                $pdo  = new \PDO($dsn, $username, $password, $options);
                $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
                $this->pdo = $pdo;
            } catch (\PDOException $e) {
                ?>
                <div class="row">
                    <div class="col-lg-12">
                        <h1 class="page-header">ERROR</h1>
                    </div>
                </div>
                <?php
                echo '<div class="alert alert-danger">Connection failed : ' . $e->getMessage().'</div>';
                exit;
            }
        } else {
            $this->pdo = $pdo;
        }
    }
    
    /* Fungsi untuk mendapatkan sinonim
     * @param string $kata
     * @return array
     */
    function getSinonimKata($kata)
    {
        $sinonims = array();
        $sql     = "SELECT * FROM sinonim WHERE LOWER(kata) = '".$kata."'";
        foreach ($this->pdo->query($sql) as $row) {
            $sinonims[$row['padanan_kata']] = $row['padanan_kata'];
        }
        
        return $sinonims;
    }
    
    //==================================================================================================================
    public function getDataSinonimAwal()
    {
        $sinonims = array();
        $sql     = "SELECT DISTINCT(`kata`) FROM sinonim";
        foreach ($this->pdo->query($sql) as $row) {
            $sinonims[$row['kata']] = $row['kata'];
        }
        
        return $sinonims;
    }
    
    public function insertDataSinonimAwal()
    {
        $sinonims = $this->getDataSinonimAwal();
        
        $no = 1;
        foreach ($sinonims as $sinonim) {
            echo $no.'. '.$sinonim.'<br/>';
            
            $sql = "INSERT INTO `kata_dasar_sinonim` (kata,jenis_kata,finished) VALUES (:kata,:jenisKata,:finished)";
            $q   = $this->pdo->prepare($sql);
            $q->execute(
                array(
                    ':kata'       => $sinonim,
                    ':jenisKata' => '',
                    ':finished'   => false
                )
            );
            
            $no++;
        }
    }
    
    public function insertNewSinonims()
    {
        $kategloService = new Kateglo;
        
        //~ $sql     = "SELECT * FROM kata_dasar_sinonim WHERE finished=false AND kata NOT IN (SELECT DISTINCT(kata) FROM `sinonim`) ORDER BY kata ASC LIMIT 0,1000";
        $sql     = "SELECT * FROM `kata_dasar_sinonim` WHERE jenis_kata = '' and kata not in (select kata from sinonim) ORDER BY kata DESC LIMIT 0,5000";
        foreach ($this->pdo->query($sql) as $row) {
            $dataKateglo = $kategloService->getLexClassNameAndSinonims($row['kata']);
            $jenisKata = '';
            if (isset($dataKateglo['lexClassName'])) {
                $jenisKata = $dataKateglo['lexClassName'];
            }
            
            if (isset($dataKateglo['sinonims'])) {
                echo '<strong>'.$row['kata'].'</strong><br/>';
                foreach ($dataKateglo['sinonims'] as $sin => $dataSinonim) {
                    echo $sin;
                    echo '<br/>';
                    
                    $sql = "INSERT INTO `sinonim` (kata,jenis_kata,padanan_kata,jenis_padanan_kata) VALUES (:kata,:jenisKata,:padananKata,:jenisPadananKata)";
                    $q   = $this->pdo->prepare($sql);
                    $q->execute(
                        array(
                            ':kata'              => strtolower($row['kata']),
                            ':jenisKata'         => strtolower($jenisKata),
                            ':padananKata'       => strtolower($dataSinonim['sinonim']),
                            ':jenisPadananKata'  => strtolower($dataSinonim['lexClass'])
                        )
                    );
                }
            }
            
            $sql = "UPDATE `kata_dasar_sinonim` SET jenis_kata=?, finished=? WHERE kata='".$row['kata']."'";
            $q = $this->pdo->prepare($sql);
            $q->execute(array($jenisKata,true));
        }
        
        
    }
    
    public function updateNewKataDasar()
    {
        $sql = "SELECT * FROM `kata_dasar` WHERE tipe = '' ORDER BY kata ASC";
        $no = 1;
        $arrKata = array();
        foreach ($this->pdo->query($sql) as $row) {
            $arrKata[$row['kata']] = $row['kata'];
            //~ $dataKateglo = $this->getLexClassNameAndSinonims($row['kata']);
            //~ $jenisKata = '';
            //~ if (isset($dataKateglo['lexClassName'])) {
                //~ $jenisKata = strtolower($dataKateglo['lexClassName']);
            //~ }
            //~ 
            //~ $sql = "UPDATE `kata_dasar` SET `tipe`=? WHERE kata='".$row['kata']."'";
            //~ $q = $this->pdo->prepare($sql);
            //~ $q->execute(array($jenisKata));
            //~ 
            echo $no.'. '.$row['kata'].'<br/>';
            $no++;
            //~ sleep(1);
        }
        var_dump($arrKata);
        if (count($arrKata) > 0) {
            $listKata = implode("', '", $arrKata);
            $sqlTipe = "SELECT * FROM `kata_dasar_local` WHERE kata in ('".$listKata."')";
            var_dump($sqlTipe);
            $no = 1;
            foreach ($this->pdo->query($sqlTipe) as $row) {
                echo $no.'. '.$row['kata'].' - '.$row['tipe'].'<br/>';
                $no++;
            }
        }
    }
    //==================================================================================================================
}

